package com.secneo.appshare.activities;

import java.io.UnsupportedEncodingException;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.common.Constants;
import com.secneo.mp.MpApi;


public class FeedBackActivity extends CommonMenuActivity
{
	private static final String SERVER_SETTING = "server_setting";
	private static final String SERVER_URL = "url";
	public void initViews()
	{
		Button mReturnBtn = (Button) findViewById(R.id.returnBtn);
		OnClickListener mReturnListener = new OnClickListener()
		{
			public void onClick(View v)
			{
				finish();
			}

		};
		mReturnBtn.setOnClickListener(mReturnListener);

		LinearLayout.LayoutParams localLayoutParams1 = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		localLayoutParams1.setMargins(10, 15, 10, 0);
		LinearLayout.LayoutParams localLayoutParams2 = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		localLayoutParams2.setMargins(10, 2, 10, 0);
		LinearLayout.LayoutParams localLayoutParams3 = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		localLayoutParams3.setMargins(10, 10, 10, 0);

		TextView localTextView1 = new TextView(this);
		localTextView1.setText(R.string.feedback_thankyou);
		localTextView1.setTextSize(12.0F);
		localTextView1.setTextColor(LinearLayout.LayoutParams.WRAP_CONTENT);
		this.fill.addView(localTextView1, localLayoutParams1);

		TextView localTextView2 = new TextView(this);
		localTextView2.setText(R.string.feedback_faq);
		localTextView2.setTextSize(16.0F);
		localTextView2.setTextColor(-6961395);
		this.fill.addView(localTextView2, localLayoutParams1);

		final EditText localEditText1 = new EditText(this);
		localEditText1.setMinLines(2);
		localEditText1.setMaxLines(5);
		fill.addView(localEditText1, localLayoutParams1);

		TextView localTextView3 = new TextView(this);
		localTextView3.setText(R.string.feedback_telephone);
		localTextView3.setTextSize(16.0F);
		localTextView3.setTextColor(-6961395);
		this.fill.addView(localTextView3, localLayoutParams1);

		final EditText localEditText4 = new EditText(this);
		localEditText4.setTextSize(16.0F);
		localEditText4.setTextColor(-6961395);
		localEditText4.setMaxLines(2);
		this.fill.addView(localEditText4, localLayoutParams2);

		Button localButton = (Button) findViewById(R.id.submit);
		localButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MpApi mp = MpApi.getAPI();
				SharedPreferences serverSaving = getSharedPreferences(SERVER_SETTING, 0);
				mp.setUserInfoURL("http://" + serverSaving.getString(SERVER_URL, "") + Constants.FEEDBACK_FOLLOW);
				String md5Imei = mp.getPhoneMd5Imei(FeedBackActivity.this);
				String strCommon = localEditText1.getText().toString();
				if(strCommon.length() == 0)
				{
					Toast.makeText(FeedBackActivity.this, getString(R.string.submit_null), Toast.LENGTH_SHORT).show();
				}
				else
				{
					String strContact = localEditText4.getText().toString();
					try
					{
						strCommon = java.net.URLEncoder.encode(strCommon, "utf-8"); 
						strContact = java.net.URLEncoder.encode(strContact, "utf-8"); 
	
					} catch (UnsupportedEncodingException e)
					{
						e.printStackTrace();
					}
					String ret = mp.submitFeedBack(Constants.PACKAGENAME, md5Imei, strCommon, strContact);
					if(ret != null)
						if(ret.equals(MpApi.SUCCESS_SUBMIT_FEEDBACK))
						{
							Toast.makeText(FeedBackActivity.this, getString(R.string.submit_ok), Toast.LENGTH_LONG).show();
							finish();
						}
						else
							Toast.makeText(FeedBackActivity.this, getString(R.string.submit_fail), Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	LinearLayout fill = null;

	public void onCreate(Bundle paramBundle)
	{
		super.onCreate(paramBundle);
		setContentView(R.layout.sec_opt_help_feedback);
		AppShareApplication.getInstance().addActivity(this);
		setTitle(R.string.help_feedback);
		fill = (LinearLayout) findViewById(R.id.fill);
		fill.setOrientation(LinearLayout.VERTICAL);
		initViews();
	}
}